package cn.stylefeng.guns.modular.api.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.cms.entity.AppNotice;
import cn.stylefeng.guns.modular.cms.model.params.*;
import cn.stylefeng.guns.modular.cms.model.result.AboutUsResult;
import cn.stylefeng.guns.modular.cms.model.result.AdResult;
import cn.stylefeng.guns.modular.cms.model.result.ContactUsResult;
import cn.stylefeng.guns.modular.cms.model.result.HelpResult;
import cn.stylefeng.guns.modular.cms.service.*;
import cn.stylefeng.guns.modular.rms.entity.Company;
import cn.stylefeng.guns.modular.rms.model.params.CompanyParam;
import cn.stylefeng.guns.modular.rms.model.params.CompanyPositionParam;
import cn.stylefeng.guns.modular.rms.model.result.CompanyPositionResult;
import cn.stylefeng.guns.modular.rms.model.result.CompanyResult;
import cn.stylefeng.guns.modular.rms.service.CompanyPositionService;
import cn.stylefeng.guns.modular.rms.service.CompanyService;
import cn.stylefeng.guns.modular.ums.entity.Member;
import cn.stylefeng.guns.modular.ums.service.MemberService;
import cn.stylefeng.guns.modular.wms.model.params.ApplyLogParam;
import cn.stylefeng.guns.modular.wms.model.params.CollectParam;
import cn.stylefeng.guns.modular.wms.service.ApplyLogService;
import cn.stylefeng.guns.modular.wms.service.CollectService;
import cn.stylefeng.guns.utils.MemberTokenUtil;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.aspectj.weaver.MemberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.function.LongToDoubleFunction;

/**
 * 公司管理相关接口
 */
@Controller
@RequestMapping("/api/rms")
@Api(tags = "公司管理相关接口")
public class ApiRmsController extends BaseController {

    @Autowired
    private CompanyService companyService;
    @Autowired
    private CompanyPositionService positionService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private CollectService collectService;
    @Autowired
    private ApplyLogService applyLogService;

    /**
     * 查询公司列表
     */
    @ResponseBody
    @RequestMapping(value = "/getCompanyList",method = RequestMethod.GET)
    @ApiOperation(value = "查询公司列表", notes = "查询公司列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", dataType = "Integer", paramType = "query")
    })
    public ResponseData getCompanyList(CompanyParam param) {
        try{
            int memberId=MemberTokenUtil.getMemberId();
            param.setMemberId(memberId);
        }catch (Exception e){
            e.printStackTrace();
        }
        LayuiPageInfo pageInfo= companyService.findPageBySpec(param);
        return ResponseData.success(pageInfo);
    }


    /**
     * 查询公司列表
     */
    @ResponseBody
    @RequestMapping(value = "/getCompanyListForSelect",method = RequestMethod.GET)
    @ApiOperation(value = "查询公司列表", notes = "查询公司列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", dataType = "Integer", paramType = "query")
    })
    public ResponseData getCompanyListForSelect(CompanyParam param) {
        LayuiPageInfo pageInfo= companyService.findPageBySpecForSelect(param);
        return ResponseData.success(pageInfo);
    }


    /**
     * 添加/更新公司信息
     */
    @ResponseBody
    @RequestMapping(value = "/updateCompany",method = RequestMethod.POST)
    @ApiOperation(value = "添加/更新公司信息", notes = "添加/更新公司信息")
    public ResponseData updateCompany(@RequestBody CompanyParam companyParam) {
        //取得当前Token登录者会员的ID
        companyService.update(companyParam);
        return ResponseData.success();
    }


    /**
     * 公司认证
     */
    @ResponseBody
    @RequestMapping(value = "/updateAuth",method = RequestMethod.POST)
    @ApiOperation(value = "公司认证", notes = "公司认证")
    public ResponseData updateAuth(@RequestBody CompanyParam companyParam) {
        //取得当前Token登录者会员的ID
        companyService.updateAuth(companyParam);
        return ResponseData.success();
    }


    /**
     * 查询公司信息
     */
    @ResponseBody
    @RequestMapping(value = "/getCompanyDetail",method = RequestMethod.GET)
    @ApiOperation(value = "查询公司信息", notes = "查询公司信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = false, paramType = "query")
    })
    public ResponseData getCompanyDetail(Long id) {
        CompanyParam companyParam=new CompanyParam();
        if(id!=null && id>0){
            companyParam.setId(id);
        }else{
            long memberId=MemberTokenUtil.getMemberId();
            Member member=memberService.getById(memberId);
            //取得当前Token登录者会员的ID
            companyParam.setId(member.getCompanyId());
        }
        CompanyResult result=companyService.findBySpec(companyParam);
        return ResponseData.success(result);
    }

    /**
     * 添加/更新职位信息
     */
    @ResponseBody
    @RequestMapping(value = "/updatePosition",method = RequestMethod.POST)
    @ApiOperation(value = "更新期望职位", notes = "更新期望职位")
    public ResponseData updatePosition(@RequestBody CompanyPositionParam positionParam) {
        //取得当前Token登录者会员的ID
        long memberId=MemberTokenUtil.getMemberId();
        Member member=memberService.getById(memberId);
        if (positionParam.getId()==null || positionParam.getId() ==0){
            positionParam.setCompanyId(member.getCompanyId());
        }
        positionParam.setCreateBy(memberId+"");
        positionService.update(positionParam);
        return ResponseData.success();
    }

    /**
     * 查询职位信息
     */
    @ResponseBody
    @RequestMapping(value = "/getPositionDetail",method = RequestMethod.GET)
    @ApiOperation(value = "查询职位信息", notes = "查询职位信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = false, paramType = "query")
    })
    public ResponseData getPostDetail(Long id) {
        CompanyPositionParam positionParam=new CompanyPositionParam();
        if(id!=null && id>0){
            positionParam.setId(id);
            CompanyPositionResult result=positionService.findBySpec(positionParam);
            //查询公司
            CompanyParam companyParam=new CompanyParam();
            companyParam.setId(result.getCompanyId());
            CompanyResult companyResult=companyService.findBySpec(companyParam);
            result.setCompanyName(companyResult.getName());
            result.setCompanyStaffSize(companyResult.getStaffSize());
            result.setIndustryName(companyResult.getIndustryName());
            result.setCompanyLogo(companyResult.getLogo());
            //查询用户
            Member member=memberService.getById(result.getCreateBy());
            result.setMemberAvatar(member.getAvatar());
            result.setMemberName(member.getName());
            result.setMemberSn(member.getMemberSn());
            result.setMemberId(member.getMemberId());
            result.setMemberPostName(member.getPostName());
            try{
                //取得当前Token登录者会员的ID
                int memberId=MemberTokenUtil.getMemberId();
                //是否收藏
                if (collectService.findBySpec(new CollectParam().setMemberId(memberId).setPositionId(id))!=null){
                    result.setIfCollect(true);
                }
                //是否投递
                if (applyLogService.findBySpec(new ApplyLogParam().setMemberId(memberId).setPositionId(id))!=null){
                    result.setIfApply(true);
                }
            }catch (Exception e){
                System.err.println(e.getMessage());
            }
            return ResponseData.success(result);
        }else{
            return ResponseData.error("参数异常");
        }
    }

    /**
     * 查询本公司职位列表
     */
    @ResponseBody
    @RequestMapping(value = "/getCompanyPositionList",method = RequestMethod.GET)
    @ApiOperation(value = "查询本公司职位列表", notes = "查询本公司职位列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", dataType = "Integer", paramType = "query")
    })
    public ResponseData getCompanyPositionList(CompanyPositionParam param) {
        if(param.getCompanyId()==null){
            //取得当前Token登录者会员的ID
            long memberId=MemberTokenUtil.getMemberId();
            Member member=memberService.getById(memberId);
            param.setCompanyId(member.getCompanyId());
        }
        LayuiPageInfo pageInfo= positionService.findPageBySpecForCom(param);
        return ResponseData.success(pageInfo);
    }



    /**
     * 查询职位列表
     */
    @ResponseBody
    @RequestMapping(value = "/getPositionList",method = RequestMethod.GET)
    @ApiOperation(value = "查询职位列表", notes = "查询职位列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "companyId", value = "公司ID", required = false, paramType = "query")
    })
    public ResponseData getPositionList(CompanyPositionParam param) {
        try{
            int memberId=MemberTokenUtil.getMemberId();
            param.setMemberId(memberId);
        }catch (Exception e){
            System.err.println(e.getMessage());
        }
        LayuiPageInfo pageInfo= positionService.findPageBySpec(param);
        return ResponseData.success(pageInfo);
    }


    /**
     * 根据ID删除公司职位
     */
    @ResponseBody
    @RequestMapping(value = "/deletePositionById",method = RequestMethod.GET)
    @ApiOperation(value = "根据ID删除公司职位", notes = "根据ID删除公司职位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query")
    })
    public ResponseData deletePositionById(Long id) {
        return ResponseData.success(positionService.removeById(id));
    }



}



